/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just-core
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: Aug 16, 2011 9:55:02 AM
 *
*/ 
package org.justframework.core.router;

import java.util.List;
import java.util.Random;

/**
 * random policy
 *
 */
public class RandomRouter<T> implements Router<T> {
	
	private List<T> nodes;
	
	public RandomRouter(){
	}
	
	public RandomRouter(List<T> nodes){
		this.nodes = nodes;
	}

	/* (non-Javadoc)
	 * @see org.justframework.core.support.HostRouter#locate(java.lang.String)
	 */
	public T locate(String... keys) {
		int size = nodes.size();
		int selectIndex = (new Random()).nextInt(size);
		return nodes.get(selectIndex);
	}

	/* (non-Javadoc)
	 * @see org.justframework.core.support.HostRouter#getHosts()
	 */
	public List<T> getNodes() {
		return nodes;
	}

	/* (non-Javadoc)
	 * @see org.justframework.core.support.HostRouter#setHosts(java.util.List)
	 */
	public void setNodes(List<T> nodes) {
		this.nodes = nodes;
	}

}
